package com.liao.badminton.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.dto.StatDto;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.badminton.entity.BoutRecord;
import com.liao.badminton.dto.BoutRecordDto;
import com.liao.badminton.vo.BoutRecordVo;
import com.liao.badminton.mapper.BoutRecordMapper;
import com.liao.badminton.service.BoutRecordService;
import com.liao.badminton.vo.StatVo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.Assert;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
/**
* @author liaocheng
* @date 2024-10-31
* description:单局详情表
*/
@Component
@Log4j2
public class BoutRecordServiceImpl  extends ServiceImpl<BoutRecordMapper, BoutRecord> implements BoutRecordService {


    /**
    * 新增
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Long> saveBoutRecord(BoutRecordDto param) throws ProjectException{
        BoutRecord boutRecord = param.invertEntity();
        EntityUtils.setCreatAndUpdateInfo(boutRecord);
        this.save(boutRecord);
        return ResponseRest.suc("saveBoutRecord", boutRecord.getId());
    }
    /**
    * 删除
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> deletesBoutRecord(BoutRecordDto param) throws ProjectException {
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "boutRecord", "id", id));
        }
        return ResponseRest.suc("deletesBoutRecord",true);
    }


    @Override
    public void deleteByImportRecordIdHard(Long importRecordId) {
        baseMapper.deleteByImportRecordId(importRecordId);
    }

    @Override
    public ResponseRest<StatVo> findTeamStat(StatDto param) {
        CheckUtil.checkValue(param,"param");
        CheckUtil.checkValue(param.getTeamOneName(),"查询队伍1名称不能为空");
        param.setTeamOneNameOrder(param.handStringOrder(param.getTeamOneName(),param.getType1(),param.getGroupId()));
        if (ObjectUtil.isNotEmpty(param.getTeamTwoName())) {
            param.setTeamTwoNameOrder(param.handStringOrder(param.getTeamTwoName(),param.getType2(),param.getGroupId()));
        }else{
            param.setTeamTwoName(null);
        }
        StatVo statVo = baseMapper.findTeamStat(param);
        if (isNotEmpty(statVo)) {
            statVo.setTeamOneName(param.getTeamOneName());
            if (ObjectUtil.isNotEmpty(param.getTeamTwoName())) {
                statVo.setTeamTwoName(param.getTeamTwoName());
            }
        }
        return ResponseRest.suc("findTeamStat",statVo);
    }

    @Override
    public ResponseRest findTeamStatImport(PageParam<StatDto> param) {
        CheckUtil.checkValue(param,"param");
        StatDto args = param.getArgs();
        CheckUtil.checkValue(args,"args");
        CheckUtil.checkValue(args.getTeamOneName(),"查询队伍1名称不能为空");
        args.setTeamOneNameOrder(args.handStringOrder(args.getTeamOneName(),args.getType1(),args.getGroupId()));
        if (ObjectUtil.isNotEmpty(args.getTeamTwoName())) {
            args.setTeamTwoNameOrder(args.handStringOrder(args.getTeamTwoName(),args.getType2(),args.getGroupId()));
        }else{
            args.setTeamTwoName(null);
        }
        PageHelper.startPage(param.getCurrent(), param.getSize());
        List<StatVo> statVos = baseMapper.findTeamStatImport(args);
        if (isNotEmpty(statVos)) {
            statVos.forEach(statVo -> {
                statVo.setTeamOneName(args.getTeamOneName());
                if (ObjectUtil.isNotEmpty(args.getTeamTwoName())) {
                    statVo.setTeamTwoName(args.getTeamTwoName());
                }
            });
        }
        return ResponseRest.suc("findTeamStatImport",new PageInfo<>(statVos));
    }

    @Override
    public ResponseRest<String> txtByImport(StatDto param) {
        CheckUtil.checkValue(param,"param");
        CheckUtil.checkValue(param.getTeamOneName(),"查询队伍1名称不能为空");
        param.setTeamOneNameOrder(param.handStringOrder(param.getTeamOneName(),param.getType1(),param.getGroupId()));
        if (ObjectUtil.isNotEmpty(param.getTeamTwoName())) {
            param.setTeamTwoNameOrder(param.handStringOrder(param.getTeamTwoName(),param.getType2(),param.getGroupId()));
        }else{
            param.setTeamTwoName(null);
        }
        List<String> txtList = baseMapper.txtByImport(param);
        if (ObjectUtil.isEmpty(txtList)) {
            return ResponseRest.fail("txtByImport", "没有找到数据");
        }
        String txt = String.join("\n", txtList);
        return ResponseRest.suc("txtByImport", txt);
    }

    /***
     * 最佳拍档
     * @param param
     * @return
     */
    @Override
    public ResponseRest findMostTeam(StatDto param) {
        Assert.notNull(param, "参数不能为空");
        Assert.notNull(param.getTeamOneName(), "查询名称不能为空");
        Assert.notNull(param.getGroupId(), "群id不能为空");
//        param.setType(21);
        List<BoutRecord> winTeams = baseMapper.findTeam(param);
//        param.setType(22);
        List<BoutRecord> loseTeams = baseMapper.findTeam(param);
        if (isNotEmpty(winTeams)) {
            for (BoutRecord winTeam : winTeams) {

            }
        }

        return null;
    }


    /**
    * 修改
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> updateBoutRecord(BoutRecordDto param)  throws ProjectException{
        BoutRecord boutRecord = param.invertEntity();
        EntityUtils.setUpdatedInfo(boutRecord);
        boolean bool = this.updateById(boutRecord);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "boutRecord", "id", param.getId());
        }
        return ResponseRest.suc("updateBoutRecord",true);
    }
    /**
    * 查询列表
    */
    @Override
    public ResponseRest findBoutRecordByPageParam(PageParam<BoutRecordDto> pageParam)  throws ProjectException{
        CheckUtil.checkValue(pageParam,"pageParam");
        ResponseRest resp = ResponseRest.suc("findBoutRecordByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize());
        LambdaQueryWrapper<BoutRecord> query = new LambdaQueryWrapper<>();
        List<BoutRecord> list = this.list(query);
        List<BoutRecordVo> collect = list.stream().parallel().map(entity -> BoutRecordVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
    * 查询详情
    */
    @Override
    public ResponseRest findDetail(BoutRecordDto param) throws ProjectException {
        Object getDate = RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "boutRecord", "id", param.getId());
        if (isNotEmpty(getDate)) {
        RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "boutRecord", "id", param.getId());
        return ResponseRest.suc("findDetail", (BoutRecordVo) getDate);
        }
        BoutRecordVo vo = null;
        BoutRecord db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = BoutRecordVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA,"boutRecord", "id", param.getId(),vo, null);
        }
        return ResponseRest.suc("findDetail", vo);
    }

}
